/**
 * 详情对话框
 */
var IntegrateInfoInfoDlg = {
    data: {
            areaCode:""
            ,areaId:""
            ,integrateType:""
            ,integrateVal:""
            ,wasteStationId:""
            ,createTime:""
            ,userId:""
    }
};
var areaNames="";
layui.use(['form', 'ax','laydate','admin',"citypicker","layer"], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var cityPicker = layui.citypicker;
    var layer = layui.layer;

    // 让当前iframe弹层高度适应
    admin.iframeAuto();
    var currentPicker = new cityPicker("#area", {
        provincename:"广东省",
        cityname:"东莞市",
        districtname: "寮步镇",
        level: '3',// 级别
    });
    var areaNextChange = function(){
        var curAreaName = $("#area").val();
        if(curAreaName!=areaNames){
            var that = $("#areaNext")[0];
            $.getJSON(Feng.ctxPath+"/region/searchReturnCode?params="+$("#area").val()+"&r="+Math.random(),function(areaMap){
                $(that).empty();
                var isFirst = true;
                for(area in areaMap){
                    if(isFirst) {
                        $("#areaCode").val(area);
                        isFirst = false;
                    }
                    $(that).html($(that).html()+"<option value='"+area+"'>"+areaMap[area]+"</option>");
                }
                form.render("select");
                layer.closeAll();
                $("#areaNext").parent().find(":text").click(function(){
                    areaNextChange();
                })
            });
            areaNames = curAreaName;
        }
    }
    $("#areaNext").parent().find(":text").click(function(){
        layer.msg('加载村数据中', {
            icon: 16
            ,shade: 0.01
        });
        areaNextChange();
    })
    form.on("select(areaFilter)",function (data) {
        console.table(data);
        $("#areaCode").val(data.value);
    })
    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var validation = new Validation($("#integrateInfoForm")[0], {onSubmit:false});
        var validate = validation.validate();
        if(validate){
            var ajax = new $ax(Feng.ctxPath + "/integrateInfo/addItem", function (data) {
                Feng.success("添加成功！");
                //window.location.href = Feng.ctxPath + "/integrateInfo";
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);

                //关掉对话框
                admin.closeThisDialog();
            }, function (data) {
                Feng.error("添加失败！" + data.responseJSON.message)
            });
            ajax.set(data.field);
            ajax.start();
            //imagePathUploader.upload();
        }else{
            var serializeArray = $(".validation-failed");
            var errMsgs = "";
            for(var i=0;i<serializeArray.length;i++){
                var elemInfo = serializeArray[i];
                var elem = $("#"+elemInfo.name)[0];
                if(elem && elem.tooltip){
                    errMsgs+="<div><span style='color:dodgerblue'>"+elem.placeholder+"输入框有误：</span><span style='color:red'>"+elem.tooltip.content+"</span></div>"
                    var tipElem = elem.tagName=="SELECT"?$(elem).next():elem;
                    layer.tips(elem.tooltip.content,tipElem,{
                        tips: [1, '#FF99CC'],
                        time: 4000,
                        tipsMore: true
                    });
                }
            }
            //Feng.error(errMsgs);
        }
        return false;
    });

    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + "/integrateInfo";
    });
    var laydate = layui.laydate;
    lay('.datetime-input,.date-input').each(function(){
        var dateType = "datetime";
        if(this.className.indexOf("datetime-input")<0){
            dateType = "date";
        }
        console.log("日期类别："+this.className+"");
        laydate.render({
            elem: this
            ,trigger: 'click'
            ,type: dateType
        });
    });
});